package com.wsj.library.resou;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.wsj.library.R;

/**
 * TODO:加载进度
 */
public class LoadingDialog extends Dialog {

    private ImageView ivImage;
    private TextView tvMsg;

    private AnimationDrawable animationDrawable;

    public LoadingDialog(Context context) {
        super(context, R.style.dialog_loading);
        init();
    }

    private void init() {
        setContentView(R.layout.dialog_loading);
        ivImage = (ImageView) findViewById(R.id.ivImage);
        tvMsg = (TextView) findViewById(R.id.tvMsg);
        initAnim();
//        getWindow().setWindowAnimations(R.anim.alpha_in);
    }

    private void initAnim() {
        ivImage.setImageResource(R.drawable.dialog_loading_animation);
        animationDrawable = (AnimationDrawable) ivImage.getDrawable();
    }

    @Override
    public void show() {
        super.show();
        animationDrawable.start();
    }

    @Override
    public void hide() {
        super.hide();
    }

    @Override
    public void dismiss() {
        super.dismiss();
        animationDrawable.stop();
    }

    @Override
    public void setTitle(CharSequence title) {
        if (!TextUtils.isEmpty(title) && null!=tvMsg) {
            tvMsg.setText(title);
        }
    }

    public static void dismissDialog(LoadingDialog loadingDialog) {
        if (null == loadingDialog) { return; }
        loadingDialog.dismiss();
    }
}